package com.xxx.dt;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/15 14:03
 * @Copyright 博为峰
 */
public class Test {
    /*
      多态(对象多态): 父类(类和接口)型指向子类对象
      多态的必备条件：
      1.继承(实现)
      2.重写
      多态的好处：解耦
      多态的缺点: 不能使用子类特有的内容

     */
    public static void main(String[] args) {
//        Student student = new Student();
//        student.eat();
//        student.study();
        //多态（对象多态）(方法多态)
//        Person student = new Student();//向上转型（自动类型提升），
//        student.eat();
//        student.study();无法再使用特有的功能
//        Student student2 = (Student) student;//向下转型
//        student2.study();

        Student student = new Student();
        show(student);

        Worker worker = new Worker();
        show(worker);
    }

    //解耦
    public static void show(Person person) {//多态
        person.eat();
//        ClassCastException 类型转换异常

        /*
          instanceof:复杂数据类型判断
         */
        if (person instanceof Student) {
            Student student = (Student) person;
            student.study();
        }
        if (person instanceof Worker) {
            Worker worker = (Worker) person;
            worker.work();
        }
    }

//    public static void show2(Worker worker){
//        worker.eat();
//        worker.work();
//    }
}
